from qiniu import Auth, put_file, etag
import qiniu.config
from qiniu import put_data


def image_storage(image_data):

    #需要填写你的 Access Key 和 Secret Key
    access_key = 'RcXzfO3eTbL2VwCELR16P9FGKmoMG-iGqu4fyMuR'
    secret_key = 'I5Dyk-zYS2qQ_OdSeQwlfrHgGi7rnVGyF1vfN2fT'

    #构建鉴权对象
    q = Auth(access_key, secret_key)

    #要上传的空间
    bucket_name = 'info016'

    #上传到七牛后保存的文件名
    # 如果不上传由七牛晕云维护
    # key = 'my-python-logo.png'
    key = None

    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    #要上传文件的本地路径
    # localfile = './mouse.jpg'
    # ret, info = put_file(token, key, localfile)
    ret, info = put_data(token, key, image_data)

    # 返回上传的状态
    if info.status_code == 200:
        return ret.get("key")
    else:
        return ""

    # print(info)
    # print(ret)
if __name__ == '__main__':

    # 方法一
    # f = open("./mouse.jpg","rb")
    # image_storage(f.read())
    # f.close()

    #方法二
    with open("./mouse.jpg","rb") as f:
        image_name = image_storage(f.read())
        print(image_name)
